// program/pages/verifier/routeDetail/index.js
const app = getApp();
const service = require('../../../utils/service.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 路线班次id
    id: '',
    // 选择的班次
    chooseTime: '',
    // 音频
    successAudioUrl: 'https://cardemo.wegouer.com/test/cars-pai/public/attachment/default/success.mp3',
    failAudioUrl: 'https://cardemo.wegouer.com/test/cars-pai/public/attachment/default/fail.mp3',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    this.setData({
      id: options.id || '',
      chooseTime: options.chooseTime
    })
  },

  // 加载详情
  loadData() {
    var that = this;
    service.getConnect(service.api.getVerifierLineDetails, {
      lineId: this.data.id,
      chooseTime: this.data.chooseTime
    }).then(function(res) {
      that.setData(res.data);
      wx.hideLoading();
    })
  },

  // 
  bindToScanCode(e) {
    let that = this;
    // 只允许从相机扫码
    console.log('============扫描二维码=============');
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ['qrCode'],
      success(res) {
        console.log(res);
        service.getConnect(service.api.driverCheckQRCode, {
          orderSn: res.result,
          time: that.data.shift,
          lineId: that.data.id
        }).then(function(res) {
          if (res.data.checkTicket === 1) {
            wx.showToast({
              title: '验票成功~',
            })
            that.resultAudioPlay(1);
            console.log(res);
            setTimeout(function() {
              that.successAudioCtx.seek(0);
              that.failAudioCtx.seek(0);
              that.bindToScanCode();
            }, 1000);
          } else {
            wx.hideLoading();
            that.resultAudioPlay(0);
            app.showConfirm('提示', res.msg, false, () => {
              console.log(res);
              that.successAudioCtx.seek(0);
              that.failAudioCtx.seek(0);
              that.bindToScanCode();
            });
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);

    // 使用 wx.createAudioContext 获取 audio 上下文 context
    this.successAudioCtx = wx.createAudioContext('successAudio', this);
    this.failAudioCtx = wx.createAudioContext('failAudio', this)
    this.successAudioCtx.obeyMuteSwitch = false;
    this.failAudioCtx.obeyMuteSwitch = false;
    this.successAudioCtx.setSrc(this.data.successAudioUrl);
    this.failAudioCtx.setSrc(this.data.failAudioUrl);
  },

  /**
   * 播放结果
   */
  resultAudioPlay(status) {
    if (status) {
      this.successAudioCtx.play();
    } else {
      this.failAudioCtx.play();
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.loadData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    // that.loadData();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let wxappStyle = wx.getStorageSync('wxappStyle');
    return {
      title: app.globalData['wxappStyle']['wxappTitle'] || wxappStyle['wxappTitle'],
      path: '/pages/index/index'
    }

  }
})